﻿using System;
using System.Threading;
using libbacktrack;
using System.IO;

namespace defical_cli
{
    partial class Program
    {
        private partial class main
        {
            public main() { throw new NotImplementedException(); }
            public main(params string[] args)
            {
                this.numThreads = Environment.ProcessorCount + 1;
                {
                    if (args.Length != 0 && !getArguments(args))
                    {
                        Console.WriteLine("Failed");
                        return;
                    }
                    printWelcome();
                    getInput();
                    printPreSummary();
                    Console.WriteLine("Started: {0}", DateTime.Now.ToString());
                    Console.WriteLine(separator);
                    go();
                    Console.WriteLine(separator);
                    Console.WriteLine("Finished: {0}", DateTime.Now.ToString());
                    //Console.ReadLine();
                }
            }
            private void printPreSummary()
            {
                Console.WriteLine(separator);
                Console.WriteLine("Graph type: {0}", this.graphType);
                Console.WriteLine("Number of vertices: {0}", this.numVer);
                Console.WriteLine("Number of deficiencies: {0}", this.numDef);
                Console.WriteLine("Algorithm: {0}", this.algorithm);
                if (this.algorithm == "backtrack")
                {
                    Console.WriteLine("Number of threads: {0}", this.numThreads);
                }
                Console.WriteLine(separator);
            }
            private void printWelcome()
            {
                Console.WriteLine("Defical_CLI v{0}", this.version);
                Console.WriteLine("Super magic deficiency calculator");
                Console.WriteLine("By Edho <me@myconan.net>");
                Console.WriteLine(separator);
            }
            private bool parseNum(string input, int min, int def, out int ret)
            {
                if (input == "")
                {
                    ret = def;
                    return true;
                }
                try { Convert.ToInt32(input); }
                catch
                {
                    ret = -1;
                    return false;
                }
                if (Convert.ToInt32(input) < min)
                {
                    ret = -1;
                    return false;
                }
                else
                {
                    ret = Convert.ToInt32(input);
                    return true;
                }
            }
            private bool parseGraphType(char i)
            {
                switch (i)
                {
                    case '1':
                    case '\r':
                        {
                            this.graphType = "wheel";
                            return true;
                        }
                    case '2':
                        {
                            this.graphType = "fan";
                            return true;
                        }
                    case '3':
                        {
                            this.graphType = "doublefan";
                            return true;
                        }
                    default:
                        { return false; }

                }
            }
            private bool parseAlgo(char i)
            {
                switch (i)
                {
                    case '1':
                    case '\r':
                        {
                            this.algorithm = "backtrack";
                            return true;
                        }
                    default:
                        { return false; }
                }
            }
            private void WriteToFile(string filePath, string line)
            {
                if (filePath == null || filePath.Length == 0)
                    return;
                if (line == null || line.Length == 0)
                    return;

                StreamWriter fileWriter = null;
                try
                {
                    if (File.Exists(filePath))
                        fileWriter = File.AppendText(filePath);
                    else
                        fileWriter = File.CreateText(filePath);
                    fileWriter.Write(line);
                }
                finally
                {
                    if (fileWriter != null)
                        fileWriter.Close();
                }
            }
            private string getHomeDir()
            {
                if (Environment.OSVersion.Platform == PlatformID.Unix ||
                   Environment.OSVersion.Platform == PlatformID.MacOSX)
                { return Environment.GetEnvironmentVariable("HOME") + "/"; }
                else
                {
                    return Environment.ExpandEnvironmentVariables("%HOMEDRIVE%%HOMEPATH%") + "\\";
                }
            }
        }
    }
}